// miniprogram/pages/audit/detail.js
Page({
  data: {
    record: {},
    sceneType: '',
    isAuditor: false,
    auditorLevel: 0,
    canAudit: false
  },

  onLoad(options) {
    const app = getApp()
    this.setData({
      sceneType: app.globalData.sceneType,
      isAuditor: app.globalData.isAuditor,
      auditorLevel: app.globalData.auditorLevel
    })
    
    if (options.id) {
      this.loadRecord(options.id)
    }
  },

  loadRecord(id) {
    // 模拟加载记录详情
    const record = {
      id: id,
      userName: '张三',
      ruleName: '完成家务',
      points: 10,
      description: '打扫客厅和厨房',
      createdAt: '2023-05-15 18:30',
      currentLevel: 1,
      maxLevel: 3,
      status: 'pending',
      auditHistory: [
        { level: 1, auditor: '审核人A', result: 'pending', time: '2023-05-15 18:30' }
      ]
    }
    
    // 判断当前用户是否有审核权限
    let canAudit = false
    if (this.data.sceneType === 'company') {
      // 公司场景：当前审核层级等于用户审核层级
      canAudit = record.currentLevel === this.data.auditorLevel
    } else {
      // 非公司场景：用户是审核人即可
      canAudit = this.data.isAuditor
    }
    
    this.setData({
      record: record,
      canAudit: canAudit
    })
  },

  approve() {
    this.performAudit('approved')
  },

  reject() {
    this.performAudit('rejected')
  },

  performAudit(result) {
    wx.showModal({
      title: result === 'approved' ? '确认通过' : '确认拒绝',
      content: result === 'approved' ? '确定要通过这条积分记录吗？' : '确定要拒绝这条积分记录吗？',
      success: (res) => {
        if (res.confirm) {
          // 模拟审核操作
          wx.showToast({
            title: result === 'approved' ? '已通过' : '已拒绝',
            icon: 'success'
          })
          
          setTimeout(() => {
            wx.navigateBack()
          }, 1500)
        }
      }
    })
  }
})